package test;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

public class ProductTableModel extends AbstractTableModel {
	private ArrayList list = new ArrayList();
	private String[] column = { "Product Name", "Product Description", "Status" };

	public ProductTableModel() {
	}

	public ProductTableModel(ArrayList list) {
		this();
		setList(list);
	}

	public int getColumnCount() {
		return column.length;
	}

	public int getRowCount() {
		return list.size();
	}

	public Object getValueAt(int arg0, int arg1) {
		ProductBean p = (ProductBean) list.get(arg0);
		return getPropertyValueByCol(p, arg1);
	}

	public void addList(int index, ProductBean p) {
		if (index < 0 || index > list.size() - 1) {
			list.add(p);
			fireTableRowsInserted(list.size(), list.size());
		} else {
			list.add(index, p);
			fireTableRowsInserted(index, index);
		}
	}

	public boolean deleteList(int index) {
		if (index >= 0 && index < list.size()) {
			list.remove(index);
			fireTableRowsDeleted(index, index);
			return true;

		} else
			return false;
	}

	public boolean updateList(int index, ProductBean p) {
		if (index >= 0 && index < list.size()) {
			list.set(index, p);
			fireTableRowsUpdated(index, index);
			return true;
		} else
			return false;
	}

	public ProductBean getList(int index) {
		if (index >= 0 && index < list.size()) {
			return (ProductBean) list.get(index);

		} else
			return null;
	}

	public ArrayList getList() {
		return list;
	}

	public void setList(ArrayList list) {
		this.list = list;
		fireTableDataChanged();
	}

	public String getColumnName(int i) {
		return column[i];
	}

	public void setColumn(String[] column) {
		this.column = column;
	}

	public Object getPropertyValueByCol(ProductBean p, int col) {
		switch (col) {
		case 0:
			return p.getProduct_name();
		case 1:
			return p.getProduct_desc();
		case 2:
			return p.getProduct_status();
		}
		return null;
	}

	public boolean isCellEditable(int row, int column) {
		return true;
	}

}
